﻿using Contracts;
using Entities;
using System.Text.RegularExpressions;
using Typesense;

namespace SearchService.MessageHandlers
{
    public class QuestionCreatedHandler(ITypesenseClient typesenseClient)
    {
        private readonly ITypesenseClient _typesenseClient = typesenseClient;

        public async Task HandleAsync(CreateQuestionCommand message)
        {
            var doc = new SearchQuestion
            {
                Id = message.Id.ToString(),
                Title = message.Title,
                Content = StripHtml(message.Content),
                CreatedAt = message.CreatedAt,
                Tags = message.Tags
            };

            await _typesenseClient.CreateDocument("questions", doc);
            Console.WriteLine("Question created and indexed in Typesense");
        }

        private string StripHtml(string content)
        {
            return Regex.Replace(content, "<.*?>", string.Empty);
        }
    }
}
